#clean up
rm(list=ls())

#options
options(scipen=10)

#load libraries
#library(foreign)
library(readxl)
library(MCMCpack)
library(lmtest)
library(car)
library(lattice)

##TIME SERIES MODELS WITH PICTURES##
hedge<-read.csv("annualData9818.csv")
hedge<-hedge[hedge$year!=2018,]
hedge[1,]

#AUM to Spending
money.aum<-lm(Dtotallobbymoney~DAUMHedgeFunds+DGDP+Dbudget+var2008,data=hedge);summary(money.aum)
money.aum.mcmc<-MCMCregress(Dtotallobbymoney~DAUMHedgeFunds+DGDP+Dbudget+var2008,data=hedge,seed=1);summary(money.aum.mcmc,quantiles = c(0.05, 0.1, 0.9,0.95))
quantile(money.aum.mcmc[,2],c(.03,.9))#0.97 probability
hedge.1<-hedge[hedge$year!=2008,]
pdf("hedgemoney.pdf")
plot(y=hedge.1$Dtotallobbymoney,x=hedge.1$DAUMHedgeFunds,type='n',xlab="Change in Hedge Fund Assets",ylab="Change in Lobby Spending")#,xlim=c(-200,700))
text(y=hedge.1$Dtotallobbymoney,x=hedge.1$DAUMHedgeFunds,labels=hedge.1$year)
abline(a=-.0803244,b=0.000351,col='red',lty=2)
dev.off()

#Spending to Returns
money.returns<-lm(allHedgeReturn~Dtotallobbymoney+var2008,data=hedge);summary(money.returns)
money.returns.mcmc<-MCMCregress(allHedgeReturn~Dtotallobbymoney+var2008,data=hedge,seed=1);summary(money.returns.mcmc,quantiles = c(0.05, 0.1, 0.9,0.95))
quantile(money.returns.mcmc[,2],c(.084,.9))#0.916 probability
pdf("moneyReturns.pdf")
plot(y=hedge.1$allHedgeReturn,x=hedge.1$Dtotallobbymoney,type='n',xlab="Change in Lobby Spending",ylab="Growth in Returns")#,xlim=c(-1750,2100))
text(y=hedge.1$allHedgeReturn,x=hedge.1$Dtotallobbymoney,labels=hedge.1$year)
abline(a=9.354,b=19.519,col='red',lty=2)
dev.off()


#Robustness check with clients
employers.aum.mcmc<-MCMCregress(Demployers~DAUMHedgeFunds+DGDP+Dbudget+var2008,data=hedge,seed=1);summary(employers.aum.mcmc,quantiles = c(0.05, 0.1, 0.9,0.95))
employers.equity.mcmc<-MCMCregress(equityIndex~Demployers+var2008,data=hedge,seed=1);summary(employers.equity.mcmc,quantiles = c(0.05, 0.1, 0.9,0.95))

#Robutness check with lobbyists
lobbyists.aum.mcmc<-MCMCregress(Dlobbyists~DAUMHedgeFunds+DGDP+Dbudget+var2008,data=hedge,seed=1);summary(lobbyists.aum.mcmc,quantiles = c(0.05, 0.1, 0.9,0.95))
lobbyists.equity.mcmc<-MCMCregress(equityIndex~Dlobbyists+var2008,data=hedge,seed=1);summary(lobbyists.equity.mcmc,quantiles = c(0.05, 0.1, 0.9,0.95))



##SPAGHETTI PLOTS##
sectors<-read.csv("sectorLobbying.csv")
sectors<-subset(sectors,subset=year!=2018)
summary(sectors)

#LOBBY EMPLOYERS
trellis.device("pdf",file="allClients.pdf",color=F)
xyplot(clients~year,data=sectors,type='l',groups=industry,xlab="Year",ylab="Number of Clients",xlim=c(1997,2024),ylim=c(0,3000))
dev.off()

top.10<-names(sort(by(data=sectors$clients,INDICES=sectors$industry,FUN=mean)))[59:68]
sectors.big<-subset(sectors,subset=industry%in%top.10)
sectors.big<-na.omit(sectors.big)

trellis.device("pdf",file="top10clients.pdf",color=F)
xyplot(clients~year,data=sectors.big,type='l',groups=industry,xlab="Year",ylab="Number of Clients",xlim=c(1997,2024),ylim=c(0,3000))
sectors.big.2017<-sectors.big[sectors.big$year==2017,]
sectors.big.2017$clients[order(sectors.big.2017$clients)]
sectors.big.2017$clients[2]<-575
sectors.big.2017$clients[10]<-675
sectors.big.2017$clients[3]<-825
sectors.big.2017$clients[8]<-1400
trellis.focus()
panel.text(x=sectors.big.2017$year,y=sectors.big.2017$clients,labels=sectors.big.2017$industry,pos=4,cex=.6)
trellis.unfocus()
dev.off()

#LOBBYISTS
trellis.device("pdf",file="allLobbyists.pdf",color=F)
xyplot(lobbyists~year,data=sectors,type='l',groups=industry,xlab="Year",ylab="Number of Lobbyists",xlim=c(1997,2024),ylim=c(0,4000))
dev.off()

top.10<-names(sort(by(data=sectors$lobbyists,INDICES=sectors$industry,FUN=mean)))[45:54]
sectors.big<-subset(sectors,subset=industry%in%top.10)
sectors.big<-na.omit(sectors.big)

trellis.device("pdf",file="top10lobbyists.pdf",color=F)
xyplot(lobbyists~year,data=sectors.big,type='l',groups=industry,xlab="Year",ylab="Number of Lobbyists",xlim=c(1997,2024),ylim=c(0,4000))
sectors.big.2017<-sectors.big[sectors.big$year==2017,]
sectors.big.2017$lobbyists[order(sectors.big.2017$lobbyists)]
sectors.big.2017$lobbyists[2]<-1000
sectors.big.2017$lobbyists[4]<-1850
sectors.big.2017$lobbyists[7]<-2000
sectors.big.2017$lobbyists[8]<-3000
trellis.focus()
panel.text(x=sectors.big.2017$year,y=sectors.big.2017$lobbyists,labels=sectors.big.2017$industry,pos=4,cex=.6)
trellis.unfocus()
dev.off()


#SPENDING
trellis.device("pdf",file="allSpend.pdf",color=F)
xyplot(spendingChained~year,data=sectors,type='l',groups=industry,xlab="Year",ylab="Billions of Dollars",xlim=c(1997,2024),ylim=c(0,.7))
dev.off()

top.10<-names(sort(by(data=sectors$spendingChained,INDICES=sectors$industry,FUN=mean)))[59:68]
sectors.big<-subset(sectors,subset=industry%in%top.10)
#sectors.big$variable<-as.character(sectors.big$variable)
sectors.big<-na.omit(sectors.big)

#trellis.device("pdf",file="top10spendCol.pdf")
trellis.device("pdf",file="top10spend.pdf",color=F)
xyplot(spendingChained~year,data=sectors.big,type='l',groups=industry,xlab="Year",ylab="Billions of Dollars",xlim=c(1997,2024),ylim=c(0,.7))
sectors.big.2017<-sectors.big[sectors.big$year==2017,]
trellis.focus()
panel.text(x=sectors.big.2017$year,y=sectors.big.2017$spendingChained,labels=sectors.big.2017$industry,pos=4,cex=.6)
trellis.unfocus()
dev.off()

